﻿using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using Module;
using Module.QuanLyDuoc.HeThongNhapXuat;

namespace HospitalManager.Webservice.QuanLyDuoc.HeThongNhapXuat
{
    /// <summary>
    ///     Summary description for wsPhieuTraNhaCungCap
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [ScriptService]
    public class wsPhieuTraNhaCungCap : WebService
    {
        private readonly PhieuTraNhaCungCap _phieuTraNhaCungCap = new PhieuTraNhaCungCap();

        [WebMethod(EnableSession = true)]
        public string PhieuTraNhaCungCap_Insert(string makho, int manhacungcap, string ngaytra, string nguoitra, string nguoinhan,
            int thue, string diengiai, string nguoitao)
        {
            var obj = new PhieuTraNhaCungCap
            {
                MaKho = makho,
                MaNhaCungCap = manhacungcap,
                NgayTra = Convert.ToDateTime(DateTime.ParseExact(ngaytra, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiTra = nguoitra,
                NguoiNhan = nguoinhan,
                Thue = thue,
                DienGiai = diengiai,
                NguoiTao = nguoitao
            };
            return _phieuTraNhaCungCap.PhieuTraNhaCungCap_Insert(obj) ? obj.MaPhieu : string.Empty;
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuTraNhaCungCap_Update(string makho, string maphieu, int manhacungcap,
            string ngaytra, string nguoitra, string nguoinhan, int thue, string diengiai, string nguoitao)
        {
            var obj = new PhieuTraNhaCungCap
            {
                MaKho = makho,
                MaPhieu = maphieu,
                MaNhaCungCap = manhacungcap,
                NgayTra = Convert.ToDateTime(DateTime.ParseExact(ngaytra, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiTra = nguoitra,
                NguoiNhan = nguoinhan,
                Thue = thue,
                DienGiai = diengiai,
                NguoiTao = nguoitao
            };
            return _phieuTraNhaCungCap.PhieuTraNhaCungCap_Update(obj);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuTraNhaCungCap_SelectById(string maphieu)
        {
            return JsonMethods.ToJsonString(_phieuTraNhaCungCap.PhieuTraNhaCungCap_SelectById(maphieu));
        }

        [WebMethod(EnableSession = true)]
        public string PhieuTraNhaCungCap_VatTuByNCC(int manhacungcap, string makho)
        {
            var data = _phieuTraNhaCungCap.PhieuTraNhaCungCap_VatTuByNCC(manhacungcap, makho);
            if (data.Rows.Count <= 0) return null;
            var list =
                data.AsEnumerable()
                    .Select(x => new { MaVatTu = x.Field<string>("MaVatTu"), TenVatTu = x.Field<string>("TenVatTu") })
                    .Distinct()
                    .ToList();
            return (new JavaScriptSerializer { MaxJsonLength = int.MaxValue }).Serialize(list);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuTraNhaCungCap_LoSanXuat_ByMaVatTu(int manhacungcap, string makho, string mavattu)
        {
            var data = _phieuTraNhaCungCap.PhieuTraNhaCungCap_VatTuByNCC(manhacungcap, makho);
            if (data.Rows.Count <= 0) return null;
            var list =
                data.AsEnumerable()
                    .Where(x => x.Field<string>("MaVatTu").Equals(mavattu))
                    .Select(
                        x => new { MaLoSanXuat = x.Field<string>("LoSanXuat"), LoSanXuat = x.Field<string>("LoSanXuat") })
                    .Distinct()
                    .ToList();
            return (new JavaScriptSerializer { MaxJsonLength = int.MaxValue }).Serialize(list);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuTraNhaCungCap_MaPhieuNhap_By_LoSanXuat(int manhacungcap, string makho, 
            string mavattu, string losanxuat)
        {
            var data = _phieuTraNhaCungCap.PhieuTraNhaCungCap_VatTuByNCC(manhacungcap, makho);
            if (data.Rows.Count <= 0) return null;
            var list =
                data.AsEnumerable()
                    .Where(row => row.Field<string>("MaVatTu") == mavattu && row.Field<string>("LoSanXuat") == losanxuat)
                    .Select(
                        x =>
                            new
                            {
                                PhieuNhap = x.Field<string>("MaPhieuNhap"),
                                MaPhieuNhap = x.Field<string>("MaPhieuNhap")
                            }).Distinct().ToList();
            return (new JavaScriptSerializer { MaxJsonLength = int.MaxValue }).Serialize(list);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuTraNhaCungCap_DonGia_ByMaPhieuNhap(int manhacungcap, string makho, 
            string mavattu, string losanxuat, string maphieunhap)
        {
            var data = _phieuTraNhaCungCap.PhieuTraNhaCungCap_VatTuByNCC(manhacungcap, makho);
            if (data.Rows.Count <= 0) return null;
            var list =
                data.AsEnumerable()
                    .Where(
                        row => row.Field<string>("MaVatTu") == mavattu && row.Field<string>("LoSanXuat") == losanxuat &&
                               row.Field<string>("MaPhieuNhap") == maphieunhap)
                    .Select(x => new { DonGia = x.Field<double>("DonGia"), HanSuDung = x.Field<DateTime>("HanSuDung") })
                    .ToList();
            return (new JavaScriptSerializer { MaxJsonLength = int.MaxValue }).Serialize(list);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuTraNhaCungCap_ChiTiet_Insert(string maphieu, string mavattu, string madonvi, int soluong, double dongia,
            string losanxuat, string hansudung, string maphieunhap, string nguoitao)
        {
            var obj = new PhieuTraNhaCungCap
            {
                MaPhieu = maphieu,
                MaVatTu = mavattu,
                MaDonVi = madonvi,
                SoLuong = soluong,
                DonGia = dongia,
                LoSanXuat = losanxuat,
                HanSuDung = Convert.ToDateTime(DateTime.ParseExact(hansudung, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                MaPhieuNhap = maphieunhap,
                NguoiTao = nguoitao
            };
            return _phieuTraNhaCungCap.PhieuTraNhaCungCap_ChiTiet_Insert(obj);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuTraNhaCungCap_ChiTiet_Update(string id, string maphieu, string mavattu, string madonvi, int soluong, double dongia,
            string losanxuat, string hansudung, string maphieunhap, string nguoitao)
        {
            var obj = new PhieuTraNhaCungCap
            {
                ID = id,
                MaPhieu = maphieu,
                MaVatTu = mavattu,
                MaDonVi = madonvi,
                SoLuong = soluong,
                DonGia = dongia,
                LoSanXuat = losanxuat,
                HanSuDung = Convert.ToDateTime(DateTime.ParseExact(hansudung, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                MaPhieuNhap = maphieunhap,
                NguoiTao = nguoitao
            };
            return _phieuTraNhaCungCap.PhieuTraNhaCungCap_ChiTiet_Update(obj);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuTraNhaCungCap_ChiTiet_Delete(string id)
        {
            return _phieuTraNhaCungCap.PhieuTraNhaCungCap_ChiTiet_Delete(id);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuTraNhaCungCap_ChiTiet_SelectById(string id)
        {
            return JsonMethods.ToJsonString(_phieuTraNhaCungCap.PhieuTraNhaCungCap_ChiTiet_SelectById(id));
        }

        [WebMethod(EnableSession = true)]
        public string PhieuTraNhaCungCap_ChiTiet_SelectByMaPhieu(string maphieu)
        {
            return JsonMethods.ToJsonString(_phieuTraNhaCungCap.PhieuTraNhaCungCap_ChiTiet_SelectByMaPhieu(maphieu));
        }
    }
}